﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoClips.WinUI.Common
{
    public class FfmepgEngine
    {

        public FfmepgEngine() { }
        public string OriginFile { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        /// <summary>
        /// 视频切割
        /// </summary>
        /// <param name="originFile">视频源文件</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public string Cut(string originFile, string startTime, string endTime)
        {
            string DstFile = originFile.Replace(".", "a.");
            string strCmd = " -ss " + startTime
                + " -i " + originFile
                + " -to " + endTime
                + " -vcodec copy -acodec copy " + DstFile + " -y ";
            if (System.IO.File.Exists(DstFile)) System.IO.File.Delete(DstFile);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "ffmpeg.exe";//要执行的程序名称
            p.StartInfo.Arguments = " " + strCmd;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = false;//可能接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = false;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = false;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = false;//不显示程序窗口

            p.Start();//启动程序
            p.WaitForExit();//等待程序执行完退出进程

            if (System.IO.File.Exists(DstFile))
            {
                return DstFile;
            }
            return "";
        }           
    }
}
